/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program;

import avenue.MyWindowEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.JFrame;
import model.DBManager;
import model.Program;
import model.ProgramRate;
import model.Station;

/**
 *
 * @author Aggelos
 */
public class EditProgramForm extends javax.swing.JFrame {

    /**
     * Creates new form EditProgramForm
     */
    public EditProgramForm() {
        em = DBManager.em;
        if ( !em.getTransaction().isActive()){
            em.getTransaction().begin();
        }
        initComponents();
    }
    
    public EditProgramForm(Program p,  boolean readOnly) {
        em = DBManager.em;
        program1=p;
        this.readOnly = readOnly;
        if ( !(em.getTransaction().isActive()) ){
            em.getTransaction().begin();
        }
        initComponents();
        //org.jdesktop.observablecollections.ObservableCollections.observableList(customerQuery.getResultList());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        query1 = java.beans.Beans.isDesignTime() ? null : em.createQuery("select s from Station s");
        list1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query1.getResultList());
        query2 = em.createQuery("select pr from ProgramRate pr where pr.stationId=:station and pr.programId=:program").setParameter("station", station2).setParameter("program", program2);
        list2 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query2.getResultList());
        TelePassPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("TelePassPU").createEntityManager();
        programQuery = java.beans.Beans.isDesignTime() ? null : TelePassPUEntityManager.createQuery("SELECT p FROM Program p");
        programList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : programQuery.getResultList();
        programRateQuery = java.beans.Beans.isDesignTime() ? null : TelePassPUEntityManager.createQuery("SELECT p FROM ProgramRate p");
        programRateList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : programRateQuery.getResultList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        programRateQuery1 = java.beans.Beans.isDesignTime() ? null : TelePassPUEntityManager.createQuery("SELECT p FROM ProgramRate p");
        programRateList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : programRateQuery1.getResultList();
        stationQuery = java.beans.Beans.isDesignTime() ? null : TelePassPUEntityManager.createQuery("SELECT s FROM Station s");
        stationList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : stationQuery.getResultList();
        Sta8mos_ = new javax.swing.JLabel();
        DiaxwristikhGrammh = new javax.swing.JSeparator();
        Titlos1 = new javax.swing.JLabel();
        Programma_ = new javax.swing.JLabel();
        Programma = new javax.swing.JTextField();
        EpiloghSta8mou = new javax.swing.JComboBox();
        Plaisio = new javax.swing.JPanel();
        Apo8hkeush = new javax.swing.JButton();
        Plaisio1 = new javax.swing.JPanel();
        Akyro = new javax.swing.JButton();
        Titlos = new javax.swing.JLabel();
        Shma = new javax.swing.JLayeredPane();
        Shma1 = new javax.swing.JLabel();
        Image = new javax.swing.JLabel();
        Shma2 = new javax.swing.JLabel();
        Logotupo = new javax.swing.JLabel();
        DiaxwristikhGrammh1 = new javax.swing.JSeparator();
        Pinakas = new javax.swing.JScrollPane();
        PinakasXrewsewnProgrammatwnAnaOxhma = new javax.swing.JTable();

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(587, 530));
        setPreferredSize(new java.awt.Dimension(550, 480));
        setResizable(false);
        getContentPane().setLayout(null);

        Sta8mos_.setFont(new java.awt.Font("Lucida Sans", 0, 12)); // NOI18N
        Sta8mos_.setForeground(new java.awt.Color(102, 0, 153));
        Sta8mos_.setText("Σταθμός: ");
        getContentPane().add(Sta8mos_);
        Sta8mos_.setBounds(20, 180, 60, 15);

        DiaxwristikhGrammh.setForeground(new java.awt.Color(102, 0, 153));
        getContentPane().add(DiaxwristikhGrammh);
        DiaxwristikhGrammh.setBounds(10, 270, 360, 10);

        Titlos1.setFont(new java.awt.Font("Lucida Sans", 0, 12)); // NOI18N
        Titlos1.setForeground(new java.awt.Color(102, 0, 153));
        Titlos1.setText("Διόδια που εισπράτονται ανά κατηγορία οχήματος");
        getContentPane().add(Titlos1);
        Titlos1.setBounds(20, 290, 310, 15);

        Programma_.setFont(new java.awt.Font("Lucida Sans", 0, 12)); // NOI18N
        Programma_.setForeground(new java.awt.Color(102, 0, 153));
        Programma_.setText("Πρόγραμμα: ");
        getContentPane().add(Programma_);
        Programma_.setBounds(20, 230, 77, 15);
        getContentPane().add(Programma);
        Programma.setBounds(110, 230, 200, 20);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, stationList, EpiloghSta8mou, "stationBoxBinding");
        bindingGroup.addBinding(jComboBoxBinding);

        EpiloghSta8mou.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EpiloghSta8mou(evt);
            }
        });
        getContentPane().add(EpiloghSta8mou);
        EpiloghSta8mou.setBounds(110, 180, 150, 20);

        Plaisio.setBackground(new java.awt.Color(255, 102, 102));

        Apo8hkeush.setFont(new java.awt.Font("Lucida Sans", 0, 12)); // NOI18N
        Apo8hkeush.setForeground(new java.awt.Color(102, 0, 153));
        Apo8hkeush.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        Apo8hkeush.setText("ΑΠΟΘΗΚΕΥΣΗ");
        Apo8hkeush.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Apo8hkeush(evt);
            }
        });
        Plaisio.add(Apo8hkeush);

        getContentPane().add(Plaisio);
        Plaisio.setBounds(430, 180, 140, 250);

        Plaisio1.setBackground(new java.awt.Color(255, 102, 102));
        Plaisio1.setForeground(new java.awt.Color(255, 102, 102));

        Akyro.setFont(new java.awt.Font("Lucida Sans", 0, 12)); // NOI18N
        Akyro.setForeground(new java.awt.Color(102, 0, 153));
        Akyro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        Akyro.setText("ΑΚΥΡΟ");
        Akyro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Akyro(evt);
            }
        });
        Plaisio1.add(Akyro);

        getContentPane().add(Plaisio1);
        Plaisio1.setBounds(430, 430, 140, 40);

        Titlos.setFont(new java.awt.Font("Lucida Sans", 1, 18)); // NOI18N
        Titlos.setForeground(new java.awt.Color(102, 0, 153));
        Titlos.setText("Διαχείριση Προγραμμάτων");
        getContentPane().add(Titlos);
        Titlos.setBounds(160, 120, 280, 30);

        Shma.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(153, 0, 0), null));
        Shma.setToolTipText("");

        Shma1.setFont(new java.awt.Font("Verdana", 0, 36)); // NOI18N
        Shma1.setForeground(new java.awt.Color(102, 0, 153));
        Shma1.setText("Tele-Pass");
        Shma1.setBounds(200, 30, 180, 40);
        Shma.add(Shma1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Image.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/toll_image1.jpg"))); // NOI18N
        Image.setToolTipText("");
        Image.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        Image.setBounds(0, 0, 180, 100);
        Shma.add(Image, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Shma2.setFont(new java.awt.Font("Lucida Sans", 0, 14)); // NOI18N
        Shma2.setForeground(new java.awt.Color(102, 0, 153));
        Shma2.setText("Automatic Toll Collection System");
        Shma2.setBounds(200, 70, 230, 30);
        Shma.add(Shma2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Logotupo.setFont(new java.awt.Font("Bradley Hand ITC", 0, 10)); // NOI18N
        Logotupo.setForeground(new java.awt.Color(102, 0, 153));
        Logotupo.setText("Developed by ANDIMAGI workgroup");
        Logotupo.setBounds(400, 0, 180, 20);
        Shma.add(Logotupo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        getContentPane().add(Shma);
        Shma.setBounds(0, 0, 580, 100);

        DiaxwristikhGrammh1.setForeground(new java.awt.Color(102, 0, 153));
        getContentPane().add(DiaxwristikhGrammh1);
        DiaxwristikhGrammh1.setBounds(10, 160, 560, 10);

        PinakasXrewsewnProgrammatwnAnaOxhma.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "ID", "Rate"
            }
        ));
        Pinakas.setViewportView(PinakasXrewsewnProgrammatwnAnaOxhma);
        PinakasXrewsewnProgrammatwnAnaOxhma.getColumnModel().getColumn(0).setHeaderValue("ID");
        PinakasXrewsewnProgrammatwnAnaOxhma.getColumnModel().getColumn(1).setHeaderValue("Rate");

        getContentPane().add(Pinakas);
        Pinakas.setBounds(10, 340, 380, 90);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void Apo8hkeush(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Apo8hkeush
        // TODO add your handling code here:
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED,true);
        for (WindowListener l : this.getWindowListeners()) {
            l.windowClosed(we);
       }
       this.setVisible(false); 
    }//GEN-LAST:event_Apo8hkeush

    private void EpiloghSta8mou(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EpiloghSta8mou
        // TODO add your handling code here:
        if(list1.size()>0 && EpiloghSta8mou.getSelectedIndex()!=-1){
            station1=(Station)(list1.get(EpiloghSta8mou.getSelectedIndex()));
            query2 = em.createQuery("select pr from ProgramRate pr where pr.stationId=:station and pr.programId=:program").setParameter("station", station1).setParameter("program", program2);
            //query2 = em.createQuery("select pr from ProgramRate pr ");
            List<ProgramRate> list3 = query2.getResultList();
            if (list3.size()>0){
                list2.clear();
                for (ProgramRate pr : list3){
                 list2.add(pr);  
               }
            } 
        }
    }//GEN-LAST:event_EpiloghSta8mou

    private void Akyro(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Akyro
        // TODO add your handling code here:
        em.getTransaction().rollback();
        em.getTransaction().begin();
        dispose();
    }//GEN-LAST:event_Akyro

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditProgramForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditProgramForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditProgramForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditProgramForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditProgramForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Akyro;
    private javax.swing.JButton Apo8hkeush;
    private javax.swing.JSeparator DiaxwristikhGrammh;
    private javax.swing.JSeparator DiaxwristikhGrammh1;
    private javax.swing.JComboBox EpiloghSta8mou;
    private javax.swing.JLabel Image;
    private javax.swing.JLabel Logotupo;
    private javax.swing.JScrollPane Pinakas;
    private javax.swing.JTable PinakasXrewsewnProgrammatwnAnaOxhma;
    private javax.swing.JPanel Plaisio;
    private javax.swing.JPanel Plaisio1;
    private javax.swing.JTextField Programma;
    private javax.swing.JLabel Programma_;
    private javax.swing.JLayeredPane Shma;
    private javax.swing.JLabel Shma1;
    private javax.swing.JLabel Shma2;
    private javax.swing.JLabel Sta8mos_;
    private javax.persistence.EntityManager TelePassPUEntityManager;
    private javax.swing.JLabel Titlos;
    private javax.swing.JLabel Titlos1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable2;
    private java.util.List<Station> list1;
    private java.util.List<ProgramRate> list2;
    private java.util.List<model.Program> programList;
    private javax.persistence.Query programQuery;
    private java.util.List<model.ProgramRate> programRateList;
    private java.util.List<model.ProgramRate> programRateList1;
    private javax.persistence.Query programRateQuery;
    private javax.persistence.Query programRateQuery1;
    private javax.persistence.Query query1;
    private javax.persistence.Query query2;
    private java.util.List<model.Station> stationList;
    private javax.persistence.Query stationQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    EntityManager em;
    ProgramRate pr;
    Program program1;
    Station station1;
    JFrame thisFrame;
    boolean readOnly;
}
